package com.example.helloworld;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.helloworld.util.ActivityUtil;

public class InvalidateListView extends Activity {
	
	private ListView mListView;
	private TestAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_invalidate_list_view);
		
		mListView =	(ListView)findViewById(R.id.lst_some);
		mAdapter =	new TestAdapter(0);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				ActivityUtil.toast(InvalidateListView.this, DATA[mAdapter.getIndex()][arg2]);
			}
		});
		
		Button button =			(Button)findViewById(R.id.btn_invalidate);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int newIndex =	(mAdapter.getIndex() + 1) % DATA.length;
				mAdapter.setIndex(newIndex);
				
				//mListView.invalidateViews();
				mAdapter.notifyDataSetChanged();
			}
		});
	}

	private static final String[][] DATA = {
		{"A001", "A002", "A003", "A004", "A005", "A006"},
		{"B001", "B002", "B003", "B004", "B005", "B006", "B007", "B008", "B009", "B010", "B011", "B012"}
	};
	
	private class TestAdapter extends BaseAdapter {
		public int mIndex =	0;  
		
		public TestAdapter() {
			super();
			
			setIndex(0);
		}
		
		public TestAdapter(int index) {
			super();
			
			setIndex(index);
		}
		
		public void setIndex(int index) {
			mIndex =	index;
		}
		
		public int getIndex() {
			return mIndex;
		}
		
		@Override
		public int getCount() {
			return DATA[mIndex].length;
		}

		@Override
		public Object getItem(int arg0) {
			return DATA[mIndex][arg0];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView view =	new TextView(getApplicationContext());
			
			view.setText(DATA[mIndex][position]);
			return view;
		}
		
	}
}
